/*
 * Copyright (c) 2020, chao
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-09-09     chao         the first version
 */

#include "emXGUI.h"
#include "gui_demo.h"

extern void GUI_Boot_Interface_DIALOG(void *param);

static void _EraseBackGround(HDC hdc, const RECT *lprc, HWND hwnd)
{
    RECT rc;

    if (lprc == NULL)
    {
        GetClientRect(hwnd, &rc);
    }
    else
    {
        CopyRect(&rc, lprc);
    }
    GUI_Printf("desktop : %d %d , %d %d\n", rc.x, rc.y, rc.w, rc.h);
    SetBrushColor(hdc, BK_COLOR(hdc));
    FillRect(hdc, &rc);
}

LRESULT desktop_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_CREATE:
    {
        SetTimer(hwnd, 1, 20, TMR_START, NULL);
        GUI_Thread_Create(GUI_Boot_Interface_DIALOG, "Boot_Interface", 8*1024, NULL, 5, 5);
        //GUI_Thread_Create(gui_user_app_entry, "gui_user", 2048, NULL, 2, 5);
        break;
    }    

    case WM_TIMER:
    {
        u16 id;

        id = LOWORD(wParam);
        if (id == 1)
        {
            GUI_InputHandler();
        }
        break;
    }

    case WM_ERASEBKGND:
    {    
        HDC hdc = (HDC)wParam;
        _EraseBackGround(hdc, NULL, hwnd);
		GUI_Printf("DESKTOP WM_ERASEBKGND\n");
        return TRUE;
    }

    default:
        return DefDesktopProc(hwnd, msg, wParam, lParam);
    }

    return WM_NULL;
}

void GUI_DesktopStartup(void)
{
    WNDCLASS wcex;
    HWND hwnd;
    MSG msg;

    wcex.Tag         = WNDCLASS_TAG;
    wcex.Style       = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = desktop_proc;
    wcex.cbClsExtra  = 0;
    wcex.cbWndExtra  = 0;
    wcex.hInstance   = NULL;
    wcex.hIcon       = NULL;
    wcex.hCursor     = NULL;

    hwnd = GUI_CreateDesktop(WS_EX_LOCKPOS, 
                             &wcex, L"DESKTOP", 
                             WS_VISIBLE|WS_CLIPCHILDREN, 
                             0, 0, GUI_XSIZE, GUI_YSIZE, 
                             NULL, 0, NULL, NULL);
    GUI_Printf("HWND_Desktop=%08xH\n", hwnd);

    ShowWindow(hwnd, SW_SHOW);

    while (GetMessage(&msg, hwnd))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}


